function [array_mask_all, array_mask_ind] = funcGenArrayMask(dir_vec, array_pos, elem_pos, elem_width, x_vec, z_vec)
%generate array mask
%
% Args:
% - dir_vec: the unit direction vector of the array transducer.
% - array_pos: the center position of the array.
% - elem_pos: the center position of each array element.
% - elem_width: the width of the array element.
% - x_vec: the x coordinates.
% - z_vec: the z coordinates.
%
% Return:
% - array_mask_all: the mask including all array element.
% - array_mask_ind: the individual mask of each element.
    step_size = mean(diff(x_vec));
    [X, Z] = meshgrid(x_vec, z_vec);
    nv = size(elem_pos, 1);
    alpha = acos(((array_pos(1) - X) * dir_vec(1) + (array_pos(2) - Z) * dir_vec(2)) ...
        ./ sqrt((X - array_pos(1)) .^ 2 + (Z - array_pos(2)) .^ 2));
    dis = sqrt((X - array_pos(1)).^2 + (Z - array_pos(2)).^2) .* sin(alpha);
    array_mask_ind = zeros(numel(z_vec), numel(x_vec), nv);
    for i = 1 : nv
        dis_elem = sqrt((X - elem_pos(i, 1)).^2 + (Z - elem_pos(i, 2)).^2);
        mask_tmp = zeros(size(X));
        mask_tmp((dis < step_size / 2) & (dis_elem < elem_width / 2)) = 1;
        array_mask_ind(:, :, i) = mask_tmp;
    end
    array_mask_all = sum(array_mask_ind, 3);
end
